import pymysql

class Dept(object):
    def __init__(self,no,name,location):
        self.no=no
        self.name=name
        self.location=location

    def __str__(self):
        return f'{self.no}\t{self.name}\t{self.location}'
def main():
    conn = pymysql.connect(host='192.168.84.89', port=3306,
                           user='root', password='Zhjai1@131waaA23',
                           db='hrs', charset='utf8',
                           cursorclass=pymysql.cursors.DictCursor)  # 字典型游标 DictCursor
    try:
        with conn.cursor() as cursor:
            cursor.execute('select dno as no,dname as name,dloc as location from tb_dept')
            for row in cursor.fetchall():
                #解包
                dept=Dept(**row)
                print(dept)


    except pymysql.MySQLError as error:
        print(error)
    finally:
        # 关闭连接，释放资源
        conn.close()


if __name__ == '__main__':
    main()
